package com.czy.blog.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czy.blog.entity.Role;
import com.czy.blog.service.RoleService;
import com.czy.blog.vo.MenuVo;
import com.czy.blog.vo.PageVo;
import com.czy.blog.vo.RoleVo;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController
{
	@Autowired
	private RoleService roleService;

	/** 进入角色管理主页面 */
	@RequestMapping("/main")
	public String main()
	{
		return "/role/role_main";
	}

	@RequestMapping("/getPage")
	@ResponseBody
	public String getPage(Model model,String roleName,PageVo pageVo)
	{
		Page<Role> roles = roleService.findByRoleName(roleName,pageVo);
		List<RoleVo> vos = users2Vos(roles);
		
		//DataGrid
		return generalDataGridJson(roles.getTotalElements(), vos);
	}

	/**
	 * List<Role>  -->  List<RoleVo>
	 * @param roles
	 * @return
	 */
	private List<RoleVo> users2Vos(Page<Role> roles)
	{
		List<RoleVo> vos = new ArrayList<RoleVo>();
		for (Role role : roles)
		{
			RoleVo vo = new RoleVo();
			BeanUtils.copyProperties(role, vo);
			vo.setCz(role.getId());
			vos.add(vo);
		}
		return vos;
	}
	
	/**
	 * 进入设置权限页面，准备菜单--权限数据
	 * @param roleId 设置权限的角色ID
	 */
	@RequestMapping("/setFuncUI/{roleId}")
	public String setFuncUI(Model model,@PathVariable String roleId){
		List<MenuVo> vos = roleService.getMenusFunctions(roleId);
		model.addAttribute("menus", vos);
		model.addAttribute("roleId", roleId);
		return "/role/role_setFunc";
	}
	
	/**
	 * 保存角色拥有的菜单和权限
	 * @param roleId 角色ID
	 * @param menuIds 拥有的菜单I集合
	 * @param funcIds 拥有的权限ID集合
	 */
	@RequestMapping("/setFunc")
	@ResponseBody
	public String setFunc(String roleId,String[] menuIds,String[] funcIds){
		roleService.saveFuncs(roleId,Arrays.asList(menuIds),Arrays.asList(funcIds));
		return "sucess";
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Model model, Role role)
	{
		roleService.save(role);
		return "success";
	}
	
	/**  进入角色添加页面*/
	@RequestMapping("/add")
	public String add(Model model)
	{
		model.addAttribute("role",new Role());
		model.addAttribute("action", "save");
		return "/role/role_edit";
	}
	
	/**  进入角色编辑页面*/
	@RequestMapping("/edit/{roleId}")
	public String edit(Model model,@PathVariable String roleId)
	{
		model.addAttribute("role",roleService.findById(roleId));
		model.addAttribute("action", "update");
		return "/role/role_edit";
	}

	@RequestMapping("/update")
	@ResponseBody
	public String update(Model model, @ModelAttribute("updateObj") Role role)
	{
		roleService.update(role);
		return "success";
	}
	
	/**  批量删除 */
	@RequestMapping("/batchDelete")
	@ResponseBody
	public String batchDelete(Model model, String[] ids) {
		roleService.delete(Arrays.asList(ids));
		return "success";
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 表单请求参数bind=true才执行，仅对update()有效（提交请求中不包含password, registerDate, salt, roles属性值，须预绑定到model）
	 */
	@ModelAttribute
	public void prepare(@RequestParam(defaultValue = "false") Boolean bindable, String id, Model model) {
		if (bindable) {
			model.addAttribute("updateObj", roleService.findById(id));
		}
	}
}
